package edu.chalmers.sdx;

import java.util.ArrayList;

import android.util.Log;
import edu.chalmers.targine.Mesh;
import edu.dhbw.andar.util.Model;

public class Skier extends Mesh{
//	private Model model;
	private ArrayList<LevelPart> levelParts;
	private float xv, yv;
	private float steerAngle;
	private String deviceID;
	private boolean toBeRemoved = false;
	
	public Skier(){
		
	}
	
	public Skier(Model model, ArrayList<LevelPart> levelParts, String deviceID){
		this.model = model;
		this.levelParts = levelParts;
		this.deviceID = deviceID;
		this.type = SkidroidXtreme.SKIER;
		xv = yv = 0f;
		steerAngle = 0f;
	}
	
	public void update(float dt){
		model.yrot += steerAngle * dt;
		model.xpos += (float)Math.cos(Math.toRadians(model.yrot)) * xv * dt;
		model.ypos += (float)Math.sin(Math.toRadians(model.yrot)) * xv * dt;
	}
	
	@Override
	public void update(){
		for(LevelPart lp : levelParts){
			if(intersects(lp)){
				model.zpos = lp.heightAt(model.xpos, model.ypos);
				Log.d("height", "ground height is " + model.zpos);
			} else{
				Log.d("height", "no collision");
			}
		}
		xv = 2f;
		model.yrot += steerAngle;
		model.xpos += (float)Math.cos(Math.toRadians(model.yrot)) * xv;
		model.ypos += (float)Math.sin(Math.toRadians(model.yrot)) * xv;
		
		model.zpos += yv;
	}
	
	public boolean intersects(LevelPart lp){
		float lpLeft = lp.getModel().xpos - 40;
		float lpRight = lp.getModel().xpos + 40;
		float lpBottom = lp.getModel().ypos - 40;
		float lpTop = lp.getModel().ypos + 40;
		float thisLeft = model.xpos - 5;
		float thisRight = model.xpos + 5;
		float thisBottom = model.ypos - 5;
		float thisTop = model.ypos + 5;
		
		if(thisRight < lpLeft || thisTop < lpBottom){
			return false;
		} else if(thisLeft > lpRight || thisBottom > lpTop){
			return false;
		} else{
			return true;
		}
	}
	
	public void finalize(){
		model.finalize();
	}
	
	public void markForRemoval(){
		toBeRemoved = false;
	}
	
	public boolean isToBeRemoved(){
		return toBeRemoved;
	}
	
	public boolean isFinalized(){
		return model.STATE == Model.STATE_FINALIZED;
	}
	
	public void setDirection(float direction){
		model.yrot = direction;
	}
	
	public void changeDirection(float steerAngle){
		this.steerAngle = steerAngle;
	}
	
	public void setPosition(float xpos, float ypos, float zpos){
		model.xpos = xpos;
		model.ypos = ypos;
//		model.zpos = zpos;
	}
	
	public void setVelocity(float xv){
		this.xv = xv;
	}
	
	public Model getModel(){
		return model;
	}
	
	public String getID(){
		return deviceID;
	}
	
	public float getXPos(){
		return model.xpos;
	}
	
	public float getYPos(){
		return model.ypos;
	}
	
	public float getZPos(){
		return model.zpos;
	}
	
	public float getAngle(){
		return model.yrot;
	}
	
	public boolean hasID(String deviceID){
		return this.deviceID.equals(deviceID);
	}

	@Override
	public boolean hasModel() {
		return model != null;
	}

}
